package com.whiteape.book.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.model.zssq.BookListDetailBean;
import com.whiteape.book.ui.adapter.RecommendDetailListAdapter;
import com.whiteape.book.ui.base.BaseActivity;
import com.whiteape.book.widget.RefreshLayout;
import com.whiteape.book.widget.itemdecoration.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class BookRecommendDetailActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener{
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.refresh_rv_content)
    RecyclerView mRvContent;

    List<BookListDetailBean.BooksBean.BookBean> data = new ArrayList<>();
    RecommendDetailListAdapter adapter;
    private Unbinder unbinder;

    @Override
    protected int getContentId() {
        return R.layout.activity_refresh_list;
    }

    @Override
    protected void initView() {
        super.initView();
//        getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
//        getWindow().getDecorView().setBackgroundColor(WaApp.getInstance().getResources().getColor(R.color.statusBarColor));
        setUpRecyclerView();
        if (data != null) {
            adapter.notifyDataSetChanged();
            refreshLayout.showFinish();
        }
    }


    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener((v)->finish());

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            String title = getIntent().getStringExtra("title");
            if (title != null) {
                getSupportActionBar().setTitle(title);
            }
//            actionBar.setTitle(R.string.category);
        }
    }

    private void setUpRecyclerView() {
        mRvContent.setLayoutManager(new LinearLayoutManager(this));
        mRvContent.addItemDecoration(new DividerItemDecoration(this));
        adapter = new RecommendDetailListAdapter(R.layout.item_book_list_book_info, data);
        adapter.setOnItemChildClickListener(this);
        mRvContent.setAdapter(adapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        Object obj = getIntent().getSerializableExtra("data");
        if (obj != null) {
            BookListDetailBean bean = (BookListDetailBean)obj;
            for(int i = 0; i < bean.getBooks().size(); i++) {
                data.add(bean.getBooks().get(i).getBook());
            }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(unbinder != null) {
            unbinder.unbind();
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        String searchKey = data.get(position).getTitle();
        Intent intent = new Intent(this, SearchActivity.class);
        intent.putExtra("searchKey", searchKey);
        startActivity(intent);
    }

//    @Override
//    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//        super.onSaveInstanceState(outState, outPersistentState);
//        outState.put
//    }
}
